package com.stylefeng.guns.modular.biz.service.impl;

import com.stylefeng.guns.common.persistence.dao.OrderInfoMapper;
import com.stylefeng.guns.common.persistence.dao.OrderItemMapper;
import com.stylefeng.guns.common.persistence.dao.ProductInfoMapper;
import com.stylefeng.guns.common.persistence.dao.WorkNoticeMapper;
import com.stylefeng.guns.common.persistence.model.OrderInfo;
import com.stylefeng.guns.common.persistence.model.OrderItem;
import com.stylefeng.guns.common.persistence.model.ProductInfo;
import com.stylefeng.guns.common.persistence.model.WorkNotice;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.OrderUtil;
import org.springframework.stereotype.Service;
import com.stylefeng.guns.modular.biz.service.IOrderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单信息Service
 *
 * @author fengshuonan
 * @Date 2017-10-17 21:13:36
 */
@Service
public class OrderServiceImpl  implements  IOrderService {

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    ProductInfoMapper      productInfoMapper;

    @Resource
    private WorkNoticeMapper workNoticeMapper;

    @Override
    public int addOrderInfo(OrderInfo orderInfo, String orderInfosValues) {
        String orderNo = OrderUtil.makeTransId();
        orderInfo.setOrderNo(orderNo);
//        if(orderInfo.getIsYumai()==2){//无需预埋
//            orderInfo.setStatus(2);//待装机
//        }else if (orderInfo.getIsYumai()==1){
//            orderInfo.setStatus(1);//待预埋
//        }
        Integer replacePeriod=-1;//更换周期(yue)
        if (orderInfosValues != null && !"".equals(orderInfosValues)) {
            String[] infos = orderInfosValues.split(",");
            OrderItem orderItem = null;
            String[] item;
            ProductInfo productInfo;
            for (int i = 0; i < infos.length; i++) {
                item = infos[i].split("-");
                orderItem = new OrderItem();
                orderItem.setOrderNo(orderNo);
                orderItem.setProductType(Integer.parseInt(item[0]));
                orderItem.setProductTypeName(item[1]);
                orderItem.setPid(Integer.parseInt(item[2]));
                productInfo = productInfoMapper.selectById(Integer.parseInt(item[2]));
                if("2".equals(productInfo.getProductType())){//超伴
                    replacePeriod=productInfo.getReplacePeriod();
                }
                if("1".equals(productInfo.getProductType())&&replacePeriod==-1){//主机且还没出现超版
                    replacePeriod=productInfo.getReplacePeriod();
                }
                orderItem.setProductModel(productInfo.getProductModel());
                orderItem.setQuantity(Integer.parseInt(item[3]));
                orderItemMapper.insert(orderItem);
            }
        }
        Integer deptid = ShiroKit.getUser().getDeptId();//获取当前用户部门id
        orderInfo.setDeptid(deptid);//部门id
        orderInfo.setCreateTime(new Date());//创建日期
        WorkNotice workNotice=null;
        if(orderInfo.getStatus() ==1){//需要预埋
            workNotice=new WorkNotice();
            workNotice.setWorkType(1);//预埋管道
            workNotice.setUserName(orderInfo.getUserName());
            workNotice.setUserPhone(orderInfo.getUserPhone());
            workNotice.setAddress(orderInfo.getAddress());
            workNotice.setDealTime(orderInfo.getYumaiTime());
            workNotice.setCreateTime(new Date());
            workNotice.setOrderNo(orderInfo.getOrderNo());
            workNotice.setDeptid(orderInfo.getDeptid());
            workNoticeMapper.insert(workNotice);//插入预埋工单
        }
        if(orderInfo.getStatus() ==1 || orderInfo.getStatus() ==2) {
            workNotice = new WorkNotice();
            workNotice.setWorkType(2);//安装
            workNotice.setUserName(orderInfo.getUserName());
            workNotice.setUserPhone(orderInfo.getUserPhone());
            workNotice.setAddress(orderInfo.getAddress());
            workNotice.setDealTime(orderInfo.getSetupTime());
            workNotice.setCreateTime(new Date());
            workNotice.setOrderNo(orderInfo.getOrderNo());
            workNotice.setDeptid(orderInfo.getDeptid());
            workNoticeMapper.insert(workNotice);//出入安装工单
        }
        if(orderInfo.getStatus() ==3){//安装完成
            if(orderInfo.getSetupTime()==null){
                orderInfo.setSetupTime(new Date());
            }
            orderInfo.setReplacePeriod(replacePeriod);
            orderInfo.setNextNiticeTime(DateUtil.getAfterMonthDate(orderInfo.getSetupTime(),replacePeriod));
        }
        int n=  orderInfoMapper.insert(orderInfo);//插入订单信息
        return n;
    }

    public OrderInfo selectOrderInfoByOrderNo(String orderNo){
        Map reqMap=new HashMap<String,Object>();
        reqMap.put("order_no",orderNo);
        List<OrderInfo> list=orderInfoMapper.selectByMap(reqMap);
        return list.get(0);
    }
}
